/***************************************************************************

Copyright (c) Microsoft Corporation. All rights reserved.
This code is licensed under the Visual Studio SDK license terms.
THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

***************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using dragonangel.phpProject.Language.DSL;
using Microsoft.VisualStudio.Package;
using Microsoft.VisualStudio.TextManager.Interop;
using Babel;
using dragonangel.phpProject.Language.DSL.Parser;

namespace dragonangel.phpProject.Language
{
    public class Resolver : Babel.IASTResolver
    {
        public Resolver(IList<Tokens> expectedTokens)
        {
            this.expectedTokens = expectedTokens;
        }

        protected IList<Tokens> expectedTokens;

        protected IVsTextView view;
        public IVsTextView View
        {
            get { return view; }
            set { view = value; }
        }

        protected AuthoringSink sink;
        public AuthoringSink Sink
        {
            get { return sink; }
            set { sink = value; }
        }


        #region IASTResolver Members

        public IList<Declaration> FindCompletions(object result, int line, int col)
        {
            CodeFile file = result as CodeFile;
            CodeExpression scope = file.GetExpression(line, col);
            List<Declaration> declarations = new List<Declaration>();
            foreach (Tokens token in expectedTokens)
                declarations.AddRange(TokenMapping.GetDeclarationsForToken(token));
            if(scope!=null)
            {
                foreach (CodeVariable variable in scope.AvailableVariablesInScope)
                    declarations.Add(new Declaration(variable.ToString(), variable.Name, (int)TokenMapping.GetGlyphForToken(Tokens.VARIABLE), variable.Name));
            }
            return declarations;
        }

        public IList<Babel.Declaration> FindMembers(object result, int line, int col)
        {
            CodeFile file = result as CodeFile;
            CodeExpression expression = file.GetFirstParentContainingMember(line, col);

            List<Babel.Declaration> members = new List<Babel.Declaration>();
            if (expression is CodeClass)
            {
                CodeClass expressionClass = expression as CodeClass;
                foreach (CodeMember member in expressionClass.Members)
                {
                    if (member is CodeField)
                        members.Add(new Declaration("", ((CodeField)member).Name, (int)Glyphs.Field | (int)((CodeMember)member).Visibility, ((CodeField)member).Name));
                    if (member is CodeFunction)
                        members.Add(new Declaration("", ((CodeFunction)member).Name, (int)Glyphs.Method | (int)((CodeMember)member).Visibility, ((CodeFunction)member).Name));

                }
            }


            // ManagedMyC.Parser.AAST aast = result as ManagedMyC.Parser.AAST;

            //foreach (string state in aast.startStates.Keys)
            //    members.Add(new Declaration(state, state, 0, state));

            return members;
        }

        public string FindQuickInfo(object result, int line, int col)
        {
            return "unknown";
        }

        public IList<Babel.Method> FindMethods(object result, int line, int col, string name)
        {
            return new List<Babel.Method>();
        }

        #endregion
    }
}
